import Dialog from '../../../../dist/dialog/dialog';
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active_name: 1,
    order_type: "",
    evaluation_show: false,
    timeList: [{
      id: 1,
      name: '付款时间'
    },{
      id: 2,
      name: '审核时间'
    },{
      id: 3,
      name: '接单时间'
    }],
    orderTabList: [{
      name: "全部订单",
      value: ""
    },{
      name: "待支付订单",
      value: "wait_pay"
    },{
      name: "待审核订单",
      value: "wait_check"
    },{
      name: "待接单订单",
      value: "wait_accept"
    },{
      name: "待服务订单",
      value: "wait_work"
    },{
      name: "已完成订单",
      value: "completed"
    },{
      name: "超时退单",
      value: "overtime"
    },{
      name: "已关闭订单",
      value: "close"
    }],
    orderList: [],
    finished: false,
    page: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getOrderData("");
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getOrderData(this.data.order_type);
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },    
  optionType(item) {
    this.setData({
      order_type: item.currentTarget.dataset.type,
      orderList: [],
      finished: false,
      page: 1,
    })
    this.getOrderData(item.currentTarget.dataset.type);
  },
  // 我要评价
  btnEvaluation() {
    this.setData({
      evaluation_show: !this.data.evaluation_show
    })
  },
  goOrderDetail(item) {
    wx.navigateTo({
      url:  `/packageJ/anytime_service/order/order_detail/index?type=${item.currentTarget.dataset.value}&order_id=${item.currentTarget.dataset.order_id}`,
    });
  },
  getOrderData (tab) {
    if (this.data.finished) return;
    let url = this.data.active_name == 1 ? 'plugin.be-within-call.frontend.order.personal-need.index' : 'plugin.be-within-call.frontend.order.enterprise-need.index'

    let baseUrl = app.getNetAddresss(url);
    app._postNetWork({
      url: baseUrl,
      data: {
        tab , 
        page: this.data.page
      },
      success: (respone) => {
        if (respone.data.result !== 1) {
          app.tips(respone.data.msg)
          return false;
        }
        let res = respone.data.data;
        let finished = false;
        let orderList = [];
        if (res.data.length === 0 || res.data.length < res.per_page || res.current_page === res.last_page) {
          finished = true;
        }
        orderList = this.data.orderList.concat(res.data);

        this.setData({
          finished,
          orderList,
          page: ++this.data.page
        });

      }
    });
  },
  evenButtonModels(ev) {
   let item = ev.currentTarget.dataset.item;
   let citem = ev.currentTarget.dataset.citem;
  //  this.order_id = item.order_id;
   this.setData({
    order_id: item.order_id
   })
    if(citem.value == 'pay') {
      wx.navigateTo({
        url: '/packageD/buy/orderPay/orderPay?order_id=' + item.order_id + '&status=2'
      });
    }else if(citem.value == 'cancel-pay' || citem.value == 'enterprise-cancel-pay') {
      let baseUrl = app.getNetAddresss(citem.api);
      app._postNetWork({
        url: baseUrl,
        data: { oid: item.order_id },
        success: ({data : { data,result,msg }}) => {
          if (result !== 1) {
            app.tips(msg)
            return false;
          }
          this.setData({
            orderList: [],
            finished: false,
            page: 1,
          })
          this.getOrderData('');
        }
      });
    }else if(citem.value == 'receive') {
      Dialog.confirm({
        title: '提示',
        message: '是否确认完成',
      })
        .then(() => {
          let baseUrl = app.getNetAddresss(citem.api);
          app._postNetWork({
            url: baseUrl,
            data: { oid: item.order_id },
            success: ({data : { data,result,msg }}) => {
              if (result !== 1) {
                app.tips(dmsg)
                return false;
              }
              this.setData({
                orderList: [],
                finished: false,
                page: 1,
              })
              this.getOrderData('');
            }
          });
        })
        .catch(() => {
        });
    }else if(citem.value == 'publish') {
      this.setData({
        evaluation_show: true
      })
    }
  },
  commentEvent() {
    this.setData({
      orderList: [],
      finished: false,
      page: 1,
    })
    this.getOrderData('');
  },
  onClick(ev) {
    let orderTabList = [];
    if(ev.detail.name == 1) {
      orderTabList = [{
        name: "全部订单",
        value: ""
      },{
        name: "待支付订单",
        value: "wait_pay"
      },{
        name: "待审核订单",
        value: "wait_check"
      },{
        name: "待接单订单",
        value: "wait_accept"
      },{
        name: "待服务订单",
        value: "wait_work"
      },{
        name: "已完成订单",
        value: "completed"
      },{
        name: "超时退单",
        value: "overtime"
      },{
        name: "已关闭订单",
        value: "close"
      }]
    }else if(ev.detail.name == 2) {
      orderTabList = [{
          name: "全部订单",
          value: ""
        },{
          name: "待支付订单",
          value: "wait_pay"
        },{
          name: "待审核订单",
          value: "wait_check"
        },{
          name: "已完成订单",
          value: "completed"
        },{
          name: "已关闭订单",
          value: "close"
        }]
    }
    this.setData({
      active_name: ev.detail.name,
      orderList: [],
      finished: false,
      page: 1,
      order_type: "",
      orderTabList
    })
    this.getOrderData('');
  }
})